package org.ucas.faker.web.service.graph;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.ucas.faker.web.utils.tech.FeatEffStatics;
import org.ucas.faker.web.dao.graph.Graph8Mapper;
import org.ucas.faker.web.dao.UserDao;
import org.ucas.faker.web.utils.graph.InstituteCount;
import org.ucas.faker.web.utils.graph.Inventor;
import org.ucas.faker.web.utils.graph.TechBranchEffect;
import org.ucas.faker.web.utils.graph.YearCount;

import javax.transaction.Transactional;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Random;

/**
 * Created by Zhang Yipin on 17-7-8.
 */
@Transactional
@Service
public class Graph8Service {

  @Autowired
  private UserDao userDao;

  
  @Autowired
  private Graph8Mapper mapper;
  

  public List< List<String>> graph_8002_1(){
	  List<String> axisX = new LinkedList<>();
	  List<String> valueY1 = new LinkedList<>();
	  List<String> valueY2 = new LinkedList<>();
	  List<String> valueY3 = new LinkedList<>();
	  
	  List<HashMap> mymap = mapper.GetKeyTechLawStatus();
	  Iterator<HashMap> it = mymap.iterator();
	  while(it.hasNext()){
		  HashMap hm = it.next();
		  axisX.add(hm.get("application_year").toString());
		  valueY1.add(((BigDecimal)hm.get("valid")).toString());
		  valueY2.add(((BigDecimal)hm.get("unvalid")).toString());
		  valueY3.add(((BigDecimal)hm.get("examination")).toString());
	  }
	  
	  List< List<String> > result= new LinkedList<>();
	  result.add(axisX);
	  result.add(valueY1);
	  result.add(valueY2);
	  result.add(valueY3);
	  
	  return result;
  }
  
  public List< List<String>> graph_8002_2(){
	  List<String> axisX = new LinkedList<>();
	  List<String> valueY = new LinkedList<>();	  

	  Iterator<HashMap> it = mapper.GetKeyTechLawStatusGroupByStatus().iterator();

	  axisX.add("有效");
	  axisX.add("失效");
	  axisX.add("审中");
	  
	  HashMap hm = it.next();
	  valueY.add(((BigDecimal)hm.get("valid")).toString());
	  valueY.add(((BigDecimal)hm.get("unvalid")).toString());
	  valueY.add(((BigDecimal)hm.get("examination")).toString());
	  

	  List< List<String> > result= new LinkedList<>();
	  result.add(axisX);
	  result.add(valueY);
	  
	  return result;
  }
  
  public List< List<String>> graph_8031(){
	  List<String> axisX = new LinkedList<>();
	  List<String> valueY = new LinkedList<>();
	  
	  List<HashMap> mymap = mapper.GetTop10KeyTechApply();
	  Iterator<HashMap> it = mymap.iterator();
	  while(it.hasNext()){
		  HashMap hm = it.next();
		  axisX.add(hm.get("applican_province").toString());
		  valueY.add(((Long)hm.get("count")).toString());
	  }
	  
	  List< List<String> > result= new LinkedList<>();
	  result.add(axisX);
	  result.add(valueY);

	  return result;
  }
  
  public List< List<String>> graph_8041(){
	  List<String> axisX = new LinkedList<>();
	  List<String> legend = new LinkedList<>();
	  

	  HashMap<String,Integer> stateMap = new HashMap<>();
	  HashMap<Integer,Integer> yearMap = new HashMap<>();  
	  
	  List<String> l = mapper.GetTop10KeyTechApplyProv();
	  //for(int i=0;i<l.size();i++){System.out.println(l.get(i));}

	  Iterator<String> its = l.iterator();
	  while(its.hasNext()){
		  String hm = its.next();
		  stateMap.put(hm,stateMap.size());
		  legend.add(hm);
	  }
	  	  
	  List<HashMap> mymap = mapper.GetTop10KeyTechApplyYearTrend();
	  List<Integer> year = new LinkedList<>();
	  Iterator<HashMap> it = mymap.iterator();
	  while(it.hasNext()){
		  HashMap hm = it.next();
		  //System.out.println(hm.get("applican_province").toString());
		  //System.out.println(hm.get("application_year").toString());
		  //System.out.println(((Long)hm.get("count")).toString());
		  if(!year.contains(Integer.parseInt(hm.get("application_year").toString().trim()))){
			  year.add(Integer.parseInt(hm.get("application_year").toString()));
		  }
	  }
	  Integer [] years = new Integer[year.size()]; 
	  year.toArray(years);
	  Arrays.sort(years);
	  for(int k:years){
		  yearMap.put(k, yearMap.size());
		  axisX.add(""+k);
	  }
	  

	  //result set
	  int [][] collect = new int[10][years.length];
	  
	  System.out.println("size+]\t"+mymap.size());
	  //parse result
	  it = mymap.iterator();
	  while(it.hasNext()){
		  HashMap hm = it.next();
		  System.out.println(hm.get("application_year").toString()+":"+yearMap.get(Integer.parseInt(hm.get("application_year").toString()))+
		  hm.get("applican_province").toString()+":"+stateMap.get(hm.get("applican_province").toString()));
		  collect[stateMap.get(hm.get("applican_province").toString())]
				  [yearMap.get(Integer.parseInt(hm.get("application_year").toString()))]
						  += ((Long)hm.get("count")).intValue();
	  }
	  /*
	   //traverse the results
	  for(int x [] : collect){
		  for(int y : x){
			  System.out.print("\t"+y);
		  }
		  System.out.println("");
	  }*/
	  
	  List< List<String> > result= new LinkedList<>();
	  result.add(axisX);
	  result.add(legend);
	  for(int x [] : collect){
		  List<String> value = new LinkedList<>();
		  for(int y : x){
			  value.add(y+"");
		  }
		  result.add(value);
	  }
	  
	  return result;
  }
  
  
  public List< List<String>> graph_8011(){
	  List<String> axisX = new LinkedList<>();
	  List<String> valueY = new LinkedList<>();
	  List<String> valueY2 = new LinkedList<>();
	  
	  List<YearCount> l1 = mapper.GetApplyYear();
	  Iterator<YearCount> it = l1.iterator();
	  if(it.hasNext()) {
		  YearCount hm = it.next();
		  axisX.add(hm.getYear());
		  valueY.add(hm.getCount().toString());
	  }
	  while(it.hasNext()){
		  YearCount hm = it.next();
		  while(Integer.parseInt(hm.getYear())>Integer.parseInt(axisX.get(axisX.size()-1))+1) {
			  axisX.add(Integer.parseInt(axisX.get(axisX.size()-1))+1+"");
			  valueY.add("0");
		  }
		  axisX.add(hm.getYear());
		  valueY.add(hm.getCount().toString());
	  }  
	  
	  List<YearCount> l2 = mapper.GetAuthorizeYear();
	  Iterator<YearCount> it2 = l2.iterator();
	  while(it2.hasNext()) {
		  YearCount yc = it2.next();
		  while(!yc.getYear().equals(axisX.get(valueY2.size()) ) ){
			  valueY2.add("0");
		  }
		  valueY2.add(yc.getCount().toString());
	  }
	  List< List<String> > result= new LinkedList<>();
	  result.add(axisX);
	  result.add(valueY);
	  result.add(valueY2);
	  
	  return result;
  }
  
  public List< List<String>> graph_8008(){
	  List<String> axisX = new LinkedList<>();
	  List<String> valueY = new LinkedList<>();
	  
	  List<HashMap> mymap = mapper.GetTop20ByProvDesc();
	  Iterator<HashMap> it = mymap.iterator();
	  while(it.hasNext()){
		  HashMap hm = it.next();
		  axisX.add(hm.get("applican_province").toString());
		  valueY.add(((Long)hm.get("count")).toString());
	  }
	  
	  List< List<String> > result= new LinkedList<>();
	  result.add(axisX);
	  result.add(valueY);
	  
	  return result;
  }

  public List< List<String>> graph_8071(){
	  List<String> axisX = new LinkedList<>();
	  List<String> axisY = new LinkedList<>();
	  List<String> valueY = new LinkedList<>();
	  
	  axisX.add("中国");
	  axisX.add("美国");
	  axisX.add("日本");
	  axisX.add("欧盟");
	  axisX.add("韩国");
	  
	  axisY.add("中国");
	  axisY.add("美国");
	  axisY.add("日本");
	  axisY.add("欧盟");
	  axisY.add("韩国");
	  
	  List< List<String> > result= new LinkedList<>();
	  
	  result.add(axisX);
	  result.add(axisY);
	  
	  Random random = new Random();
	  for(int i=0;i<5;i++) {
		  List<String> l = new LinkedList<>();
		  for(int j=0;j<5;j++) {
			  l.add(""+random.nextInt(200));
		  }
		  result.add(l);
	  }
	  random.nextInt(200);
	  
	  return result;
  }
  
  public List< List<String>> graph_8101(){
	  List<String> axisX = new LinkedList<>();
	  List<String> legend = new LinkedList<>();
	  

	  HashMap<String,Integer> stateMap = new HashMap<>();
	  HashMap<Integer,Integer> yearMap = new HashMap<>();  
	  
	  List<String> l = mapper.GetTop10KeyTechApplyProvHome();
	  //for(int i=0;i<l.size();i++){System.out.println(l.get(i));}

	  Iterator<String> its = l.iterator();
	  while(its.hasNext()){
		  String hm = its.next();
		  stateMap.put(hm,stateMap.size());
		  legend.add(hm);
	  }
	  	  
	  List<HashMap> mymap = mapper.GetTop10KeyTechApplyHomeTrend();
	  List<Integer> year = new LinkedList<>();
	  Iterator<HashMap> it = mymap.iterator();
	  while(it.hasNext()){
		  HashMap hm = it.next();
		  //System.out.println(hm.get("applican_province").toString());
		  //System.out.println(hm.get("application_year").toString());
		  //System.out.println(((Long)hm.get("count")).toString());
		  if(!year.contains(Integer.parseInt(hm.get("application_year").toString().trim()))){
			  year.add(Integer.parseInt(hm.get("application_year").toString()));
		  }
	  }
	  Integer [] years = new Integer[year.size()]; 
	  year.toArray(years);
	  Arrays.sort(years);
	  for(int k:years){
		  yearMap.put(k, yearMap.size());
		  axisX.add(""+k);
	  }
	  

	  //result set
	  int [][] collect = new int[10][years.length];
	  
	  System.out.println("size+]\t"+mymap.size());
	  //parse result
	  it = mymap.iterator();
	  while(it.hasNext()){
		  HashMap hm = it.next();
		  System.out.println(hm.get("application_year").toString()+":"+yearMap.get(Integer.parseInt(hm.get("application_year").toString()))+
		  hm.get("applican_province").toString()+":"+stateMap.get(hm.get("applican_province").toString()));
		  collect[stateMap.get(hm.get("applican_province").toString())]
				  [yearMap.get(Integer.parseInt(hm.get("application_year").toString()))]
						  += ((Long)hm.get("count")).intValue();
	  }
	  /*
	   //traverse the results
	  for(int x [] : collect){
		  for(int y : x){
			  System.out.print("\t"+y);
		  }
		  System.out.println("");
	  }*/
	  
	  List< List<String> > result= new LinkedList<>();
	  result.add(axisX);
	  result.add(legend);
	  for(int x [] : collect){
		  List<String> value = new LinkedList<>();
		  for(int y : x){
			  value.add(y+"");
		  }
		  result.add(value);
	  }
	  
	  return result;
  }
  
  
  public List< List<String>> graph_8111(){
	  List<String> axisX = new LinkedList<>();
	  List<String> valueY = new LinkedList<>();
	  
	  List<InstituteCount> list = mapper.GetTop20Institute();
	  Iterator<InstituteCount> it = list.iterator();
	  while(it.hasNext()){
		  InstituteCount ic = it.next();
		  axisX.add(ic.getApplicant());
		  valueY.add(ic.getAppcount()+"");
	  }
	  
	  List< List<String> > result= new LinkedList<>();
	  result.add(axisX);
	  result.add(valueY);
	  
	  return result;
  }
  
  public List< List<String>> graph_8121(){
	  List<String> axisX = new LinkedList<>();
	  List<String> legend = new LinkedList<>();
	  

	  HashMap<String,Integer> stateMap = new HashMap<>();
	  HashMap<Integer,Integer> yearMap = new HashMap<>();  
	  
	  List<String> l = mapper.GetTop10InstApply();
	  //for(int i=0;i<l.size();i++){System.out.println(l.get(i));}

	  Iterator<String> its = l.iterator();
	  while(its.hasNext()){
		  String hm = its.next();
		  stateMap.put(hm,stateMap.size());
		  legend.add(hm);
	  }
	  	  
	  List<HashMap> mymap = mapper.GetTop10InstApplyTrend();
	  List<Integer> year = new LinkedList<>();
	  Iterator<HashMap> it = mymap.iterator();
	  while(it.hasNext()){
		  HashMap hm = it.next();
		  //System.out.println(hm.get("applican_province").toString());
		  //System.out.println(hm.get("application_year").toString());
		  //System.out.println(((Long)hm.get("count")).toString());
		  if(!year.contains(Integer.parseInt(hm.get("application_year").toString().trim()))){
			  year.add(Integer.parseInt(hm.get("application_year").toString()));
		  }
	  }
	  Integer [] years = new Integer[year.size()]; 
	  year.toArray(years);
	  Arrays.sort(years);
	  for(int k:years){
		  yearMap.put(k, yearMap.size());
		  axisX.add(""+k);
	  }
	  

	  //result set
	  int [][] collect = new int[10][years.length];
	  //parse result
	  it = mymap.iterator();
	  while(it.hasNext()){
		  HashMap hm = it.next();
		  collect[stateMap.get(hm.get("ap").toString())]
				  [yearMap.get(Integer.parseInt(hm.get("application_year").toString()))]
						  += ((Long)hm.get("count")).intValue();
	  }	  
	  List< List<String> > result= new LinkedList<>();
	  result.add(axisX);
	  for(int i=0;i<legend.size();i++) {
		  String s = legend.get(i);
		  legend.set(i, legend.get(i).substring(0,s.length()>15?15:s.length()));
		  //System.out.println(legend.get(i));
	  }
	  result.add(legend);
	  for(int x [] : collect){
		  List<String> value = new LinkedList<>();
		  for(int y : x){
			  value.add(y+"");
		  }
		  result.add(value);
	  }
	  
	  return result;
  }
  
  
  public List< List<String>> graph_8015(){
	  List<String> axisX = new LinkedList<>();
	  List<String> valueY1 = new LinkedList<>();
	  List<String> valueY2 = new LinkedList<>();
	  List<String> valueY3 = new LinkedList<>();
	  
	  List<HashMap> mymap = mapper.GetTop10ByLegalState();
	  Iterator<HashMap> it = mymap.iterator();
	  while(it.hasNext()){
		  HashMap hm = it.next();
		  axisX.add(hm.get("ap").toString());
		  valueY1.add(((BigDecimal)hm.get("examination")).toString());
		  valueY2.add(((BigDecimal)hm.get("unvalid")).toString());
		  valueY3.add(((BigDecimal)hm.get("valid")).toString());
	  }
	  
	  List< List<String> > result= new LinkedList<>();
	  result.add(axisX);
	  result.add(valueY1);
	  result.add(valueY2);
	  result.add(valueY3);
	  
	  return result;
  }
  
  public Map<String,List<String> > branch_effect(){
		FeatEffStatics fes = new FeatEffStatics();
		List<TechBranchEffect> tbe = mapper.GetBranchAndEffect();
		List<List<String> > features = new ArrayList<>();
		List<List<String> > effects = new ArrayList<>();
		for(TechBranchEffect aaa : tbe){
			List<String> fea = new ArrayList<String>();
			List<String> eff = new ArrayList<String>();
			
			for(String s:aaa.getTech_branch().split("#")){
				fea.add(s);
			}
			for(String s:aaa.getTech_effect().split("#")){
				eff.add(s);
			}
			features.add(fea);
			effects.add(eff);
			//System.out.println("#"+aaa.getTech_branch()+"#"+aaa.getTech_effect()+"#");
			//fes.
		}
		
		return fes.CountFeaturesAndEffects(features,effects);
  }
  public List< List<String>> graph_8014(){
	  List<String> axisX = new LinkedList<>();
	  List<String> valueY = new LinkedList<>();
	  
	  
	  List<Inventor> inventors = mapper.GetInventorsWithYears();
	  //
	  HashMap<String, HashMap<String,Integer>> rs = new HashMap<>();
	  Iterator<Inventor> it = inventors.iterator();
	  while(it.hasNext()){
		  Inventor inv = it.next();
		  //System.out.println(inv.getInventor());
		  String ints []=  inv.getInventor().split("\\|");
		  for(String s:ints){
			  s = s.trim();
			  //System.out.println(s);
			  HashMap<String,Integer> hm;
			  if(rs.containsKey(s)){
				  hm = rs.get(s);
			  }else{
				  hm = new HashMap<>();
				  rs.put(s, hm);
			  }
			  if(hm.containsKey(inv.getApplicant())){
				  hm.put(inv.getApplicant(), hm.get(inv.getApplicant()).intValue()+1);				  
			  }else{
				  hm.put(inv.getApplicant(),1);
			  }
		  }
	  }
	  
	  ArrayList<InventorStatics> array = new ArrayList<>();
	  int thre20 = 0;
	  array.add(new InventorStatics("","",0));
	  for(String t:rs.keySet()){
		  HashMap<String,Integer> t2 = rs.get(t);
		  for(String app:t2.keySet()){
			  int number = t2.get(app);
			  //System.out.println(t+"\t"+app+"\t"+number);
			  if(number>thre20){
				  int pos = array.size();
				  for(pos = array.size();pos>0;pos--){
					  if(array.get(pos-1).count>number) break;
				  }
				  array.add(pos, new InventorStatics(t,app,number));
				  if(array.size()>20){
					  while(array.size()>20) array.remove(array.size()-1);
					  thre20 = array.get(19).count;
				  }
			  }
		  }
	  }
	  
	  for(int i=0;i<array.size();i++){
		  System.out.println(array.get(i).Inventor+"\t"+array.get(i).applicant+"\t"+array.get(i).count);
		  axisX.add(array.get(i).Inventor);
		  valueY.add(""+array.get(i).count);
	  }
	  
	  List< List<String> > result= new LinkedList<>();
	  result.add(axisX);
	  result.add(valueY);
	  
	  return result;
  }
  class InventorStatics{
		public InventorStatics(String inventor, String applicant, int count) {
			super();
			Inventor = inventor;
			this.applicant = applicant;
			this.count = count;
		}
		public String Inventor;
		public String applicant;
		public int count;
	}

  
}